var userInfo;

init();

function init() {
    //1.查询项目列表
    queryProjectList(0);
}

function queryProjectList(pageIndex) {
    $.ajax({
        url: prefixUrl + "project/queryList",
        type: "post",
        data: JSON.stringify({pageIndex:pageIndex}),
        contentType: "application/json;",
        success: function (data) {
            var projectHtml = "";
            var list = data.data.rows;
            for(var i=0; i<list.length; i++){
                projectHtml = projectHtml
                    +'<option style="text-align: center;" value="'
                    +list[i].projectId
                    +'">'
                    +list[i].projectName
                    +'</option>';
            }
            $("#projectList").html(projectHtml);
            //2.查询用户列表
            queryUserList(0,$("#projectList").val());
        }
    })
}

/**
 * 一级材料类型监听事件
 */
$("#projectList").change(function(){
    var projectId = $("#firstMaterial").val();
    queryUserList(0,projectId);
});

function queryUserList(pageIndex,projectId) {
    var name = $("#name").val();
    if(pageIndex == null){
        pageIndex = 0;
    }
    if(projectId == null || projectId == ""){
        projectId = $("#projectList").val();
    }
    $.ajax({
        url :   prefixUrl + "user/getUserList",
        type:   "post",
        data:JSON.stringify({
            userName : name,
            projectId:projectId,
            pageIndex : pageIndex
        }),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            var list = data.data.rows;
            var scheduleHtml  = '<thead>'
                +'<tr>'
                +'<th>序号</th>'
                +'<th>员工名称</th>'
                +'<th>员工性别</th>'
                +'<th>员工电话	</th>'
                +'<th>操作</th>'
                +'</tr>'
                +'</thead>'
                +'<tbody>';
            for(var i=0; i<list.length; i++){
                var sexName = "男";
                if(list[i].userSex == 1){
                    sexName = "女";
                }
                scheduleHtml = scheduleHtml
                    +'<tr>'
                    +'<td>' + (parseInt(i) + 1) + '</td>'
                    +'<td>'
                    +list[i].userName
                    +'</td>'
                    +'<td>'
                    +sexName
                    +'</td>'
                    +'<td>'
                    +list[i].userPhone
                    +'</td>'
                    +'<td>'
                    +'<button type="button" class="btn btn-info mar-r-5" data-toggle="modal" data-target="#exampleModal" data-whatever="@mdo" onclick=queryUserDetail("'
                    + list[i].userId
                    +'")><i class="fa fa-pencil"></i></button>'
                    +'<button type="button" class="btn btn-info" onclick=delteUserInfo("'
                    +list[i].userId
                    +'")><i class="fa fa-bitbucket-square"></i></button>'
                    +'</td>'
                    +'</tr>';
            }
            scheduleHtml = scheduleHtml + +'</tbody>';
            $("#userList").html(scheduleHtml);

            getNumberList(data);
        }
    });
}

function updateUserInfo() {
    var updateData = {
        userId:userInfo.userId,
        userName : $("#userName").val(),
        projectId : $("#projectList").val(),
        userPhone : $("#userPhone").val(),
        userRole : $("input[name='userRole']:checked").val(),
        loginName : $("#loginName").val(),
        loginPassword : $("#loginPassword").val()
    };
    $.ajax({
        url :   prefixUrl + "user/updateRole",
        type:   "post",
        data:JSON.stringify(updateData),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            alert(data.msg);
            queryUserList(0);
            modalDisplay();
        }
    });
}

function delteUserInfo(id) {
    if(confirm("确定要进行删除嘛?")){
        $.ajax({
            url :   prefixUrl + "user/delete",
            type:   "post",
            data:JSON.stringify({userId:id}),
            contentType:"application/json;",
            success:function(data){
                goToLogin(data);
                alert(data.msg);
                queryUserList(0);
            }
        });
    }
}

function modalDisplay(){
    $(".modal-backdrop").css("display","none");
    $(".modal").css("display","none");
}

function queryUserDetail(userId) {
    var projectId = $("#projectList").val();
    $.ajax({
        url :   prefixUrl + "user/queryDetail",
        type:   "post",
        data:JSON.stringify({
            userId : userId,
            projectId:projectId
        }),
        contentType:"application/json;",
        success:function(data){
            goToLogin(data);
            userInfo = data.data;
            var detail = data.data;
            clearUserDetail();
            $("#userName").val(detail.userName);
            $("#userPhone").val(detail.userPhone);
            $(":radio[name='userRole'][value='" + detail.userRole + "']").prop("checked", "checked");
            $("#loginName").val(detail.loginName);
            $("#loginPassword").val(detail.loginPassword);
        }
    });
}

function clearUserDetail(){
    $("#userName").val("");
    $("#userPhone").val("");
    $('input:radio[name="userRole"]').removeAttr('checked');
    $("#loginName").val("");
    $("#loginPassword").val("");
}
